package com.xiaoxin.experience.api.tree;

import com.xiaoxin.experience.api.tree.model.DirectoryInfo;
import com.xiaoxin.experience.api.tree.model.InstanceInfo;
import com.xiaoxin.experience.common.RestResponse;
import com.xiaoxin.experience.service.tree.TreeService;
import com.xiaoxin.experience.service.tree.common.JsTree;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author xiaoxin
 */
@RestController
@RequestMapping("/api/tree")
public class TreeController
{
     @Resource
     private TreeService treeService;

     @GetMapping("/list")
     public RestResponse<List<JsTree>> getInstanceTree()
     {
         return RestResponse.success(treeService.getInstanceTree());
     }

     @PostMapping("/directory")
     public RestResponse<Boolean> saveOrUpdateDirectory(@RequestBody DirectoryInfo directoryInfo)
     {
          if (Objects.nonNull(directoryInfo.getId()) && directoryInfo.getId() == 1)
          {
               return RestResponse.success(false);
          }
         return RestResponse.success(treeService.saveOrUpdate(directoryInfo));
     }

     @PostMapping("/instance")
     public RestResponse<Boolean> saveOrUpdateInstance(@RequestBody InstanceInfo instanceInfo)
     {
         return RestResponse.success(treeService.saveOrUpdate(instanceInfo));
     }

     @DeleteMapping("/directory/{id}")
     public RestResponse<Boolean> deleteDirectory(@PathVariable Integer id)
     {
          if (Objects.isNull(id) || id == 1)
          {
              return RestResponse.success(false);
          }
         return RestResponse.success(treeService.deleteDirectory(id));
     }

     @DeleteMapping("/instance/{id}")
     public RestResponse<Boolean> deleteInstance(@PathVariable Integer id)
     {
         return RestResponse.success(treeService.deleteInstance(id));
     }
}
